/*
 *  ValidationHelper.cs
 *  
 *  [Description of the use of this file.]
 *  
 *  Copyright (c) 2009, Richard Bao Labs.
 *  
 *  The contents of this file are subject to the Mozilla Public License
 *  Version 1.1 (the "License"); you may not use this file except in
 *  compliance with the License. You may obtain a copy of the License at
 *  
 *  http://www.mozilla.org/MPL/
 *  
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 *  the License for the specific language governing rights and limitations
 *  under the License.
 *  
 *  The Original Code is RichControlLibrary.
 *  
 *  The Initial Developer of the Original Code is Richard Bao Labs.
 *  All Rights Reserved.
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace RichControlLibrary.Internal
{
    internal class ValidationHelper : IDisposable
    {
        public ValidationHelper(ITextControl text)
        {
            _text = text;

            _text.Validating += new CancelEventHandler(_text_Validating);
        }

        ~ValidationHelper()
        {
            Dispose();
        }

        private ITextControl _text = null;

        private Validator _validator = null;
        public Validator Validator
        {
            get { return _validator; }
            set { _validator = value; }
        }

        private bool _validateWhenTyping = false;
        public bool ValidateWhenTyping
        {
            get { return _validateWhenTyping; }
            set
            {
                _validateWhenTyping = value;

                IPromptText promptText = _text as IPromptText;
                if (promptText != null)
                {
                    if (_validateWhenTyping)
                    {
                        promptText.PromptTextChanged += new EventHandler(_text_TextChanged);
                    }
                    else
                    {
                        promptText.PromptTextChanged -= new EventHandler(_text_TextChanged);
                    }
                }
                else
                {
                    if (_validateWhenTyping)
                    {
                        _text.TextChanged += new EventHandler(_text_TextChanged);
                    }
                    else
                    {
                        _text.TextChanged -= new EventHandler(_text_TextChanged);
                    }
                }
            }
        }

        private void _text_Validating(object sender, CancelEventArgs e)
        {
            PerformValidate();
        }

        private void _text_TextChanged(object sender, EventArgs e)
        {
            PerformValidate();
        }

        public bool PerformValidate()
        {
            if (_validator != null)
            {
                // Get the content
                string content = null;

                IPromptText promptText = _text as IPromptText;
                if (promptText != null)
                {
                    content = promptText.ValueText;
                }
                else
                {
                    content = _text.Text;
                }

                // Validate
                ValidationResult result = _validator.Validate(content);
                if (result.Success)
                {
                    if (_validator.SupportsAutoFormatting)
                    {
                        _text.Text = result.AutoCorrectedText;
                    }

                    OnValidInput(EventArgs.Empty);
                    return true;
                }
                else
                {
                    OnInvalidInput(EventArgs.Empty);
                    return false;
                }

            }
            else
            {
                return true;
            }
        }

        public event EventHandler InvalidInput;
        protected virtual void OnInvalidInput(EventArgs e)
        {
            if (InvalidInput != null)
            {
                EventHandler handler = InvalidInput;
                handler(this, e);
            }
        }

        public event EventHandler ValidInput;
        protected virtual void OnValidInput(EventArgs e)
        {
            if (ValidInput != null)
            {
                EventHandler handler = ValidInput;
                handler(this, e);
            }
        }

        #region IDisposable Members

        public void Dispose()
        {
            _text.Validating -= new CancelEventHandler(_text_Validating);
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
